<script setup lang="ts">
import { useRouter } from 'vue-router';
import { PhCaretLeft, PhHouse } from '@phosphor-icons/vue';

defineProps<{
  code?: string;
  title?: string;
  message?: string;
}>();

const router = useRouter();

const goBack = () => {
  router.back();
};

const goHome = () => {
  router.push('/');
};
</script>

<template>
  <div class="error-page" :data-error="code || '500'">
    <div class="error-content">
      <!-- 错误代码 -->
      <div
        class="error-illustration"
        v-motion
        :initial="{ opacity: 0, x: -50 }"
        :enter="{ opacity: 1, x: 0, transition: { delay: 200 } }"
      >
        <!-- 修改这里，确保显示传入的 code -->
        <div class="error-code">{{ code || '500' }}</div>
      </div>

      <!-- 错误信息 -->
      <div
        class="error-info"
        v-motion
        :initial="{ opacity: 0, x: 50 }"
        :enter="{ opacity: 1, x: 0, transition: { delay: 400 } }"
      >
        <h1>{{ title || '服务器出错了' }}</h1>
        <p>{{ message || '抱歉，服务器开小差了，请稍后再试' }}</p>
        <div class="error-actions">
          <button class="btn-back" @click="goBack">
            <PhCaretLeft :size="17" color="#0d0d0d" weight="light" />
            <span>返回上页</span>
          </button>
          <button class="btn-home" @click="goHome">
            <PhHouse :size="17" color="#FAFAFA" weight="light" />
            <span>回到首页</span>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.error-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f6f8ff 0%, #ffffff 100%);
  overflow: hidden;

  .error-content {
    display: flex;
    gap: 60px;
    align-items: center;
    max-width: 800px;
    padding: 60px;
    background: #ffffff;
    border-radius: 30px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.05);
  }

  .error-illustration {
    position: relative;

    .error-code {
      font-size: 120px;
      font-weight: 700;
      color: #ef4444;
      text-shadow: 0 2px 10px rgba(239, 68, 68, 0.2);
    }

    &::after {
      content: '';
      position: absolute;
      bottom: -10px;
      left: 50%;
      transform: translateX(-50%);
      width: 60%;
      height: 4px;
      background: linear-gradient(90deg, transparent, #ef4444, transparent);
      border-radius: 2px;
      opacity: 0.3;
    }
  }

  .error-info {
    h1 {
      font-size: 32px;
      color: #ef4444;
      margin-bottom: 16px;
      font-weight: 600;
    }

    p {
      font-size: 16px;
      color: #6b7280;
      margin-bottom: 32px;
      line-height: 1.6;
    }

    .error-actions {
      display: flex;
      gap: 16px;

      button {
        padding: 12px 24px;
        border-radius: 12px;
        font-size: 16px;
        display: flex;
        align-items: center;
        gap: 8px;
        cursor: pointer;
        transition: all 0.3s ease;
        border: none;

        .icon {
          font-size: 18px;
          line-height: 1;
        }

        &.btn-back {
          background: #f1f5f9;
          color: #475569;

          &:hover {
            background: #e2e8f0;
            transform: translateY(-2px);
          }
        }

        &.btn-home {
          background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
          color: #ffffff;

          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(239, 68, 68, 0.2);
            opacity: 0.9;
          }
        }
      }
    }
  }
}

// 针对不同错误代码的颜色变化
.error-page {
  &[data-error="403"] {
    .error-illustration .error-code {
      color: #f59e0b;
      text-shadow: 0 2px 10px rgba(245, 158, 11, 0.2);
    }
    .error-info h1 { color: #f59e0b; }
    .error-illustration::after { background: linear-gradient(90deg, transparent, #f59e0b, transparent); }
    .btn-home { background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%) !important; }
  }

  &[data-error="503"] {
    .error-illustration .error-code {
      color: #8b5cf6;
      text-shadow: 0 2px 10px rgba(139, 92, 246, 0.2);
    }
    .error-info h1 { color: #8b5cf6; }
    .error-illustration::after { background: linear-gradient(90deg, transparent, #8b5cf6, transparent); }
    .btn-home { background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%) !important; }
  }
}

// 暗色模式
@media (prefers-color-scheme: dark) {
  .error-page {
    background: linear-gradient(135deg, #1a1a2e 0%, #1e1e24 100%);

    .error-content {
      background: rgba(255, 255, 255, 0.05);
      backdrop-filter: blur(10px);
    }

    .error-info {
      p {
        color: rgba(255, 255, 255, 0.7);
      }

      .error-actions {
        .btn-back {
          background: rgba(255, 255, 255, 0.1);
          color: rgba(255, 255, 255, 0.8);

          &:hover {
            background: rgba(255, 255, 255, 0.15);
          }
        }
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .error-page {
    .error-content {
      flex-direction: column;
      text-align: center;
      padding: 40px 24px;
      gap: 40px;
      margin: 20px;
    }

    .error-illustration {
      .error-code {
        font-size: 80px;
      }
    }

    .error-info {
      .error-actions {
        flex-direction: column;

        button {
          width: 100%;
          justify-content: center;
        }
      }
    }
  }
}
</style>
